//
//  ZYPendingUseController.m
//  BestLearning
//
//  Created by Ethan on 2017/9/21.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYPendingUseController.h"
#import "ZYPendingUseCell.h"
#define kCell @"cell"
#import "ZYPendingUseModel.h"
@interface ZYPendingUseController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;
@end

@implementation ZYPendingUseController





- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorRGB(247, 245, 245);
    
  
    [self setUpSuperView];
    [self setUpRefresh];
    
    
}

#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
     [self.dataSource removeAllObjects];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetwork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetwork];
    
    
}
- (void)setNetwork{

    NSString *uid =[NSString stringWithFormat:@"%@", [AccountTool shareAccountTool].currentAccount.uid];
    

    
 NSString *url = [NSString stringWithFormat:@"coupon.do?action=myCoupon&uid=%@&status=1&pageNumber=%ld",uid,(long)self.page];
    
    NSLog(@"%@  %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            NSArray *arr = [t valueForKey:@"dataList"];
            
            for (NSDictionary *dic in arr) {
                
                ZYPendingUseModel *model = [ZYPendingUseModel new];
                model.ID = [dic valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataSource addObject:model];
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
        }
    } Fail:^(NSError *Error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    

    
}



- (void)setUpSuperView{
    
    UIView *allVie = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    allVie.backgroundColor = ColorRGB(247, 245, 245);
  
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, SCREENH_HEIGHT-64-40)];
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ZYPendingUseCell  class] forCellReuseIdentifier:kCell];
    self.tableView.estimatedRowHeight = 200;
      self.tableView.tableHeaderView = allVie;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYPendingUseCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.dataSource.count > indexPath.row) {
        ZYPendingUseModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYPendingUseModel:model];
        
      
    }
    
      [cell.exchangeButton addTarget:self action:@selector(exchangeButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}


#pragma mark --- shangchufangfa de shix
- (void)exchangeButtonAction:(UIButton *)sender{
    ZYPendingUseCell *cell = (ZYPendingUseCell *)sender.superview.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYPendingUseModel *model = self.dataSource[indexPath.row];
    
    ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否立即兑换?" preferredStyle:(UIAlertControllerStyleAlert)];
    ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if (_dataSource.count > 1) {
            
            
            NSMutableArray *temp = [NSMutableArray arrayWithArray:_dataSource];
            
            [temp removeObjectAtIndex:indexPath.row];
            
            _dataSource = temp;
            
            NSArray *deleteItems = @[indexPath];
            
            [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];
            
        }else{
            [_dataSource removeAllObjects];
            [self.tableView reloadData];
        }
        
       
        [self netMoonWorkId:model.ID];
        
        
    }];
    
    ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    action.textColor = [UIColor blackColor];
    actionn.textColor = [UIColor blackColor];
    [alert addAction:action];
    [alert addAction:actionn];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
    
    
    
    
}







- (void)netMoonWorkId:(NSString *)Id{
    
    NSString *url = [NSString stringWithFormat:@"coupon.do?action=changeStatus&id=%@&status=3",Id];
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
}



































- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    
}





@end
